package org.mindrive.system.service.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mindrive.system.collection.schedule.ScheduleDetailsEntity;
import org.mindrive.system.collection.system.UserEntity;
import org.mindrive.system.parameter.schedule.ScheduleDetailsParameter;
import org.mindrive.system.parameter.schedule.ScheduleDetailsSearchParameter;
import org.mindrive.system.dto.ScheduleDetailsDTO;
import org.mindrive.system.service.BaseService;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * (ScheduleDetails)表服务接口
 *
 * @author maweihong
 * @since 2023-12-11 09:41:44
 */
@Service
@Slf4j
public class ScheduleDetailsService extends BaseService {

    /**
     * 查询(可分页)
     * @Author mwh
     * @Date 13:48 2023/8/12
     * @Param [parameter]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String, Object> search(ScheduleDetailsSearchParameter parameter) {
        QueryWrapper<ScheduleDetailsEntity> query = new QueryWrapper<ScheduleDetailsEntity>();
        query.eq("zhuang_tai" , "0");

                
        if (parameter.getId() != null && !"".equals(parameter.getId())) {
            query.eq("ID" , parameter.getId());
        }
                
        if (parameter.getScheName() != null && !"".equals(parameter.getScheName())) {
            query.eq("SCHE_NAME" , parameter.getScheName());
        }
                
        if (parameter.getMonValue() != null && !"".equals(parameter.getMonValue())) {
            query.eq("MON_VALUE" , parameter.getMonValue());
        }
                
        if (parameter.getTuesValue() != null && !"".equals(parameter.getTuesValue())) {
            query.eq("TUES_VALUE" , parameter.getTuesValue());
        }
                
        if (parameter.getWedValue() != null && !"".equals(parameter.getWedValue())) {
            query.eq("WED_VALUE" , parameter.getWedValue());
        }
                
        if (parameter.getThursValue() != null && !"".equals(parameter.getThursValue())) {
            query.eq("THURS_VALUE" , parameter.getThursValue());
        }
                
        if (parameter.getFriValue() != null && !"".equals(parameter.getFriValue())) {
            query.eq("FRI_VALUE" , parameter.getFriValue());
        }
                
        if (parameter.getSatValue() != null && !"".equals(parameter.getSatValue())) {
            query.eq("SAT_VALUE" , parameter.getSatValue());
        }
                
        if (parameter.getSunValue() != null && !"".equals(parameter.getSunValue())) {
            query.eq("SUN_VALUE" , parameter.getSunValue());
        }
                
        if (parameter.getCreator() != null && !"".equals(parameter.getCreator())) {
            query.eq("CREATOR" , parameter.getCreator());
        }
                
        if (parameter.getCreatedAt() != null && !"".equals(parameter.getCreatedAt())) {
            query.eq("CREATED_AT" , parameter.getCreatedAt());
        }
                
        if (parameter.getUpdator() != null && !"".equals(parameter.getUpdator())) {
            query.eq("UPDATOR" , parameter.getUpdator());
        }
                
        if (parameter.getUpdateTime() != null && !"".equals(parameter.getUpdateTime())) {
            query.eq("UPDATE_TIME" , parameter.getUpdateTime());
        }
                
        int count = (int) scheduleDetailsDao.count(query);
        query.orderByAsc(CREATED_AT);
        
        List<ScheduleDetailsEntity> list = new ArrayList<ScheduleDetailsEntity>();
        List<ScheduleDetailsDTO> listDto = new ArrayList<ScheduleDetailsDTO>();
        Page<ScheduleDetailsEntity> page = page(ScheduleDetailsEntity.class, parameter.getPageSize(), parameter.getCurrentPage());
        if (page == null) {
            list = scheduleDetailsDao.list(query);
        } else {
            scheduleDetailsDao.page(page, query);
            list = page.getRecords();
        }
        
        for (ScheduleDetailsEntity scheduleDetails : list){
            ScheduleDetailsDTO scheduleDetailsDTO = new ScheduleDetailsDTO();
            BeanUtils.copyProperties(scheduleDetails , scheduleDetailsDTO);
            listDto.add(scheduleDetailsDTO);
        }
        
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(COUNT, count);
        result.put(LIST, listDto);
        return result;
    }
    
    public ScheduleDetailsEntity create(ScheduleDetailsParameter parameter) {
        final UserEntity loginUser = getLoginUser();
        ScheduleDetailsEntity scheduleDetails = new ScheduleDetailsEntity();
        BeanUtils.copyProperties(parameter, scheduleDetails);
//        scheduleDetails.setZhuangTai("0");
        scheduleDetails.setCreator(loginUser.getUserName());
        scheduleDetails.setCreatedAt(new Date());
        scheduleDetailsDao.save(scheduleDetails);
        return scheduleDetails;
    }
    
    public void modify(String id, ScheduleDetailsParameter parameter) {
        final UserEntity loginUser = getLoginUser();
        ScheduleDetailsEntity scheduleDetails = new ScheduleDetailsEntity();
        BeanUtils.copyProperties(parameter, scheduleDetails);
        scheduleDetails.setId(id);
        scheduleDetails.setUpdator(loginUser.getUserName());
        scheduleDetails.setUpdateTime(new Date());
        scheduleDetailsDao.updateById(scheduleDetails);
    }

    public void delete(List<String> ids) {
        final UserEntity loginUser = getLoginUser();
        for (String id : ids){
            ScheduleDetailsEntity obj = scheduleDetailsDao.getById(id);
//            obj.setZhuangTai("1");
            obj.setUpdator(loginUser.getUserName());
            obj.setUpdateTime(new Date());
            scheduleDetailsDao.updateById(obj);
        }
    }
    
    public void deletePhysics(List<String> ids) {
        scheduleDetailsDao.removeByIds(ids);
    }
    
}

